//
//  WmcShareManager.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/4/5.
//

import Foundation
import UIKit

// MARK: - [WmcShareManager]

class WmcShareManager: NSObject {
    // Lifecycle

    override private init() {}

    // Internal

    enum Scene: Int32 {
        /// 聊天界面
        case session = 0
        /// 朋友圈
        case friendCircle = 1
        /// 收藏
        case favorite = 2
        /// 指定联系人
        case specifiedSession = 3
    }

    /// 分享文本
    /// - Parameters:
    ///   - text: 标题
    ///   - description: 描述
    ///   - scene: 场景
    ///   - completion: 分享结果
    static func sendText(_ text: String,
                         description: String? = nil,
                         scene: Scene,
                         completion: ((Bool) -> Void)? = nil)
    {
        guard WmcWechatShare.isInstalled() else {
            installedWechatAlert()
            return
        }

        WmcWechatShare.shareText(text,
                                 description: description,
                                 scene: scene.rawValue) { isSuccess in
            completion?(isSuccess)
        }
    }

    /// 分享图片
    /// - Parameters:
    ///   - image: 图片
    ///   - title: 标题
    ///   - description: 描述
    ///   - messageExt: 额外描述
    ///   - scene: 场景
    ///   - completion: 分享结果
    static func shareImage(_ image: UIImage,
                           title: String? = nil,
                           description: String? = nil,
                           messageExt: String? = nil,
                           scene: Scene,
                           completion: ((Bool) -> Void)? = nil)
    {
        guard WmcWechatShare.isInstalled() else {
            installedWechatAlert()
            return
        }

        WmcWechatShare.share(image,
                             title: title,
                             description: description,
                             messageExt: messageExt,
                             scene: scene.rawValue) { isSuccess in
            completion?(isSuccess)
        }
    }

    /// 分享链接
    /// - Parameters:
    ///   - linkUrl: 链接
    ///   - title: 标题
    ///   - description: 描述
    ///   - thumbImage: 缩略图
    ///   - scene: 场景
    ///   - completion: 分享结果
    static func shareLink(_ linkUrl: String,
                          title: String,
                          description: String? = nil,
                          thumbImage: UIImage? = nil,
                          scene: Scene,
                          completion: ((Bool) -> Void)? = nil)
    {
        guard WmcWechatShare.isInstalled() else {
            installedWechatAlert()
            return
        }

        WmcWechatShare.shareLink(linkUrl,
                                 title: title,
                                 description: description,
                                 thumbImage: thumbImage,
                                 scene: scene.rawValue) { isSuccess in
            completion?(isSuccess)
        }
    }

    static func shareFile(_ path: String,
                          fileExtension: String,
                          title: String,
                          description: String? = nil,
                          thumbImage: UIImage? = nil,
                          scene: Scene,
                          completion: ((Bool) -> Void)? = nil)
    {
        guard WmcWechatShare.isInstalled() else {
            installedWechatAlert()
            return
        }
        var size: Float = 0
        if let fileAttributes = try? FileManager.default.attributesOfItem(atPath: path),
           let fileSize = fileAttributes[FileAttributeKey.size]
        {
            size = (fileSize as! NSNumber).floatValue
        }
        let mb = size / 1024.0 / 1024.0
        if mb >= 10 {
            WmcHudManager.toast("文件超过微信分享限制，暂时无法分享")
        }else{
            WmcWechatShare.shareFile(path,
                                     fileExtension: fileExtension,
                                     title: title,
                                     description: description,
                                     thumbImage: thumbImage,
                                     scene: scene.rawValue) { isSuccess in
                completion?(isSuccess)
            }
        }
    }

    static func installedWechatAlert() {
        let alert = WmcAlertView()
        alert.title = "温馨提示"
        alert.msg = "请先安装微信App"
        alert.style = .confirm(title: "确定")
        alert.show()
    }
    
    /// 打开微信小程序
    /// - Parameters:
    ///   - username: 小程序id
    ///   - path: 打开小程序的路径
    ///   - completion: 分享回调
    static func openWXMiniProgram(username: String, path: String, completion: ((Bool) -> Void)? = nil) {
        guard WmcWechatShare.isInstalled() else {
            installedWechatAlert()
            return
        }
        let request = WXLaunchMiniProgramReq()
        request.userName = username
        request.path = path
        request.miniProgramType = miniProgramType
        WXApi.send(request, completion: completion)
    }
    
    /// 小程序环境
    private static var miniProgramType: WXMiniProgramType {
        switch YPWmcManager.shared.environmentType {
        case .release:
            return .release
        default:
            return .preview
        }
    }
}

extension WmcShareManager {
    
    /// 打开微信小程序,关注公众号
    static func followWechat() {
        let appId = "gh_f39e342c00a3"
        var url = "https://mp.weixin.qq.com/s/-nLaLhG2SacxpbP2NdlXwQ"
        url = url.yp.urlEncoded ?? ""
        let path = "/subpackage/web-view/index/index?url=\(url)"
        openWXMiniProgram(username: appId, path: path)
    }
}
